//
//  XNPublicResourceViewController.m
//  XINNingOA
//
//  Created by samuel on 2022/2/14.
//

#import "XNPublicResourceViewController.h"
#import "XNPublicResourceBillInfoRequest.h"
#import "XNPublicResourceApproveRequest.h"
#import "XNPublicResourceBillInfoModel.h"

@interface XNPublicResourceViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XNPublicResourceBillInfoModel *infoModel;

@end

@implementation XNPublicResourceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [SVProgressHUD show];
    __weak typeof(self)weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    XNPublicResourceBillInfoRequest *infoRequest = [[XNPublicResourceBillInfoRequest alloc] initWithOrderId:self.orderId];
    [infoRequest startWithCompletionBlockWithSuccess:^(__kindof XNPublicResourceBillInfoRequest * _Nonnull request) {
        weakSelf.infoModel = request.infoModel;
        dispatch_group_leave(group);
    } failure:^(__kindof XNPublicResourceBillInfoRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    XNPublicResourceApproveRequest *approveRequest = [[XNPublicResourceApproveRequest alloc] initWithOrderId:self.orderId];
    [approveRequest startWithCompletionBlockWithSuccess:^(__kindof XNPublicResourceApproveRequest * _Nonnull request) {
        weakSelf.approveArray = request.approveFlowArray;
        dispatch_group_leave(group);
    } failure:^(__kindof XNPublicResourceApproveRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, queue, ^{
        [weakSelf creatTableData];
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        });

    });
    
}

- (void)creatTableData
{
    XNTableShowDataModel *infoModel = [[XNTableShowDataModel alloc] init];
    infoModel.showType = XNShowType_Normal;
    infoModel.title = @"单据信息";
    XNShowItemModel *itemModel = [[XNShowItemModel alloc] init];
    NSArray *showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"单据编号： %@",self.infoModel.ID],
                              [NSString stringWithFormat:@"登记日期： %@",self.infoModel.createTimeString],
                              [NSString stringWithFormat:@"申请人： %@",self.infoModel.applicantName],
                              [NSString stringWithFormat:@"申请部门： %@",self.infoModel.departmentName],
                              [NSString stringWithFormat:@"职务： %@",self.infoModel.positionName],
                              [NSString stringWithFormat:@"备注： %@",self.infoModel.remark],
                              nil];
    itemModel.showItemArray = showItemArray;
    [infoModel.showDataArray addObject:itemModel];
    [self.showArray addObject:infoModel];
    
    XNTableShowDataModel *showModel3 = [[XNTableShowDataModel alloc] init];
    showModel3.showType = XNShowType_Normal;
    showModel3.title = @"子表信息";
    [self.infoModel.itemList enumerateObjectsUsingBlock:^(XNPublicResourceModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *itemModel3 = [[XNShowItemModel alloc] init];
        NSArray *showItemArray3 = [NSArray arrayWithObjects:
                                       [NSString stringWithFormat:@"物资类别: %@",obj.publicResourceName],
                                       [NSString stringWithFormat:@"物资名称： %@",obj.publicResourceItemName],
                                       [NSString stringWithFormat:@"规格型号： %@",obj.brand],
                                   [NSString stringWithFormat:@"尺寸： %@",obj.size.length > 0 ? obj.size : @""],
                                   [NSString stringWithFormat:@"单位: %@",obj.unit.length > 0 ? obj.unit : @""],
                                   [NSString stringWithFormat:@"数量： %.3f",obj.num],
                                   [NSString stringWithFormat:@"备注: %@",obj.remark.length > 0 ? obj.remark : @""],
                                       nil];
        itemModel3.showItemArray = showItemArray3;
        [showModel3.showDataArray addObject:itemModel3];
    }];
    [self.showArray addObject:showModel3];
    
    XNTableShowDataModel *approveModel = [[XNTableShowDataModel alloc] init];
    approveModel.showType = XNShowType_Approve;
    approveModel.title = @"审批流程";
    [self.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:obj];
        [approveModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:approveModel];
}

@end
